import Vue from 'vue'

import {getToken} from "../util/auth";

import Router from 'vue-router'
import Home from "../components/Home";
import Login from "../components/Login";
import Users from "../components/user/Users";
import Roles from "../components/user/Roles";
import Privileges from "../components/user/Privileges";
import Products from "../components/prod/Products";
import Brands from "../components/prod/Brands";
import LoginLogs from "../components/log/LoginLogs";
import OperationLogs from "../components/log/OperationLogs";
import OrderList from "../components/order/OrderList";
import OrderSetting from "../components/order/OrderSetting";
import RejectReason from "../components/order/RejectReason";
import Categories from "../components/prod/Categories";
import RejectProcess from "../components/order/RejectProcess";
import Welcome from "../components/Welcome";
import Seckill from "../components/market/Seckill";
import Coupon from "../components/market/Coupon";
import BrandRecommend from "../components/market/BrandRecommend";
import NewGoodsRecommend from "../components/market/NewGoodsRecommend";
import PopularRecommend from "../components/market/PopularRecommend";
import TopicRecommend from "../components/market/TopicRecommend";
import AdvertisementList from "../components/market/AdvertisementList";

Vue.use(Router)

const router = new Router({
  routes: [

    {
      path:'/',
      redirect:'/login'
    },

    //首页路由
    {
      path:'/home',
      name:'Home',
      component:Home,
      redirect: '/welcome',
      children:[{path:'/welcome',component:Welcome},
        {path:'/users/management',component:Users},
        {path:'/roles/management',component:Roles},
        {path:'/privileges/management',component:Privileges},
        {path:'/product/category',component:Categories},
        {path:'/product/management',component:Products},
        {path:'/brands/management',component:Brands},
        {path:'/log/login',component:LoginLogs},
        {path:'/log/operation',component:OperationLogs},
        {path:'/orders/list',component:OrderList},
        {path:'/orders/set',component:OrderSetting},
        {path:'/rejected/reason',component:RejectReason},
        {path:'/rejected/process',component:RejectProcess},
        {path:'/seckill',component:Seckill},
        {path:'/coupon',component:Coupon},
        {path:'/brand/recommend',component:BrandRecommend},
        {path:'/newgoods/recommend',component:NewGoodsRecommend},
        {path:'/popular/recommend',component:PopularRecommend},
        {path:'/topic/recommend',component:TopicRecommend},
        {path:'/advertisement/list',component:AdvertisementList}
      ]
    },

    //登录页路由
    {
      path:'/login',
      name:"Login",
      component:Login
    }
  ]
})

//路由导般守卫
router.beforeEach((to,from,next)=>{
  const token = getToken();
  //如果目标是登录路由，则直接放行
  if(to.path == '/login'){

    if(token){
      return next('/home');
    }else{
      return next();
    }

  }else {
    //否则根据token是否存在来拦载
    //获取token
    if (token) {
      return next();
    } else {
      next('/login');
    }
  }
});


export default router;
